﻿

using RFIDData.Data;
using RFIDData.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Phoenix_RFID
{
	public partial class EmployeeRegister : Form
	{
		Dbman dm = new Dbman();
		string emImage;
		public EmployeeRegister()
		{
			InitializeComponent();
		}

		private void btnRegister_Click(object sender, EventArgs e)
		{
			FileStream stream = new FileStream(emImage, FileMode.Open);
			byte[] buffer = new byte[stream.Length];
			stream.Read(buffer, 0, buffer.Length);//BYTE[]에 파일 내용(이미지) 저장
			bool? sex = true;
			if (rdobtnF.Checked)
			{
				sex = false;
			}
			DialogResult dialogResult = MessageBox.Show("가입 하시겠습니까?", "사원 등록", MessageBoxButtons.YesNo);
			if (dialogResult == DialogResult.Yes)
			{
				Employee emp = new Employee
				{
					Name = txtName.Text,
					Sex = sex,
					Phone = txtP1.Text + "-" + txtP2.Text + "-" + txtP3.Text,
					Department = cboDepartment.SelectedItem.ToString(),
					Rank = cboDepartment.SelectedItem.ToString(),
					HireDate = dtpHireDate.Value,
					Image = buffer
				};
				dm.EmployeeAdd(emp);
				this.Close();
			}
			else if (dialogResult == DialogResult.No)
			{
				this.Close();
			}
		}
		
		private void btnImage_Click(object sender, EventArgs e)
		{
			OpenFileDialog dialog = new OpenFileDialog();
			dialog.Filter = "JPG|*.jpg|GIF|*.gif|PNG|*.png|BMP|*.bmp"; // file types, that will be allowed to upload
			dialog.Multiselect = false;	// allow/deny user to upload more than one file at a time
			
			if (dialog.ShowDialog() == DialogResult.OK)	// if user clicked OK
			{
				FileStream stream = new FileStream(dialog.FileName, FileMode.Open);

				pbImage.Image = Image.FromStream(stream);
				emImage = dialog.FileName;
				stream.Close();
				
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void EmployeeRegister_Load(object sender, EventArgs e)
		{
			cboDepartment.Items.Add("경영과");
			cboDepartment.Items.Add("인사과");
			cboDepartment.Items.Add("총무과");
			cboDepartment.Items.Add("영업팀");
			cboDepartment.Items.Add("개발팀");
			cboDepartment.Items.Add("기획팀");
			cboDepartment.SelectedIndex = 0;

			cboRank.Items.Add("사장");
			cboRank.Items.Add("부사장");
			cboRank.Items.Add("상무");
			cboRank.Items.Add("이사");
			cboRank.Items.Add("부장");
			cboRank.Items.Add("과장");
			cboRank.Items.Add("차장");
			cboRank.Items.Add("대리");
			cboRank.Items.Add("팀장");
			cboRank.Items.Add("사원");
			cboRank.SelectedIndex = 0;
		}
	}
}
